package com.csh.szhm.fragment;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.csh.szhm.R;
import com.csh.szhm.SzhmApplication;
import com.csh.szhm.SzhmApplication.locationImpl;
import com.csh.szhm.activity.SzhmSelectCityActivity;
import com.csh.szhm.activity.SzhmShopDetailActivity;
import com.csh.szhm.adapter.SzhmMainAdapter;
import com.csh.szhm.custorm.likenetease.LikeNeteasePull2RefreshListView;
import com.csh.szhm.http.HttpRequestTool;
import com.csh.szhm.http.HttpUrls;
import com.csh.szhm.module.recommend.MainRecommenListBean;
import com.csh.szhm.utils.StringUtil;
import com.csh.szhm.utils.sharepreferences.SharedPreferencesUtil;
import com.google.gson.reflect.TypeToken;

public class MainFragmentOne extends MyBaseFragment<MainRecommenListBean>
		implements OnClickListener {

	private LikeNeteasePull2RefreshListView mListView;
	private SzhmMainAdapter mSzhmMainAdapter;
	private TextView mCityName;
	private String mCurrentCityId;

	public MainFragmentOne(int view) {
		super(view);
	}

	@Override
	protected void init() {
		// initCenterView();
		requestNet();
		mListView = (LikeNeteasePull2RefreshListView) getActivity()
				.findViewById(R.id.szhm_main_listview);
		mCityName = (TextView) getActivity().findViewById(
				R.id.main_one_city_txt_id);
		mSzhmMainAdapter = new SzhmMainAdapter(getActivity());
		mListView.setAdapter(mSzhmMainAdapter);
		getActivity().findViewById(R.id.main_select_city).setOnClickListener(
				this);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent in = new Intent();
				in.putExtra("id", mSzhmMainAdapter.getItem(arg2 - 3)
						.getFirmId());
				startMyActivity(in, SzhmShopDetailActivity.class);
			}
		});
		getLocation();
	}

	private void getLocation() {
		mCurrentCityId = SharedPreferencesUtil.getInstance().getCityId();
		if (StringUtil.isEmptyString(mCurrentCityId)) {
			SzhmApplication.getInstance().mLocationClient.start();
			SzhmApplication.getInstance().setLocationImpl(new locationImpl() {

				@Override
				public void getLocation(String city) {
					// TODO Auto-generated method stub
					mCityName.setText(city);
				}
			});
		}
	}

	@Override
	public void onClick(View v) {
		startMyActivity(SzhmSelectCityActivity.class);
	}

	@Override
	protected void requestNet() {
		if (mHttpRequestTool == null) {
			mHttpRequestTool = new HttpRequestTool<MainRecommenListBean>();
			mTypeToken = new TypeToken<MainRecommenListBean>() {
			};
			mHttpRequestTool.setHttpCallBackUi(this);
		}
		mHttpRequestTool.cloneRequest(Method.GET, HttpUrls.mMainRecommend,
				mTypeToken, getClass().getSimpleName(), "MAIN");
		super.requestNet();
	}

	@Override
	public void onResume() {
		String city = SharedPreferencesUtil.getInstance().getCityName();
		if (!StringUtil.isEmptyString(city)) {
			mCityName.setText(city);
		}
		if (!mCurrentCityId.equals(SharedPreferencesUtil.getInstance()
				.getCityId())) {
			// 刷新界面
			mCurrentCityId = SharedPreferencesUtil.getInstance().getCityId();
			
		}
		super.onResume();
	}

	@Override
	public void success(MainRecommenListBean data) {
		if (data != null) {
			if (data.getTopDataList() != null
					&& data.getTopDataList().size() > 0) {
				mSzhmMainAdapter.addData(data.getTopDataList());
			}
		}
		super.success(data);
	}

}
